package com.kust.smarthome.web.controller;

import com.kust.smarthome.web.imservice.Imservice;
import com.kust.smarthome.web.model.*;
import com.kust.smarthome.web.service.*;
import com.kust.smarthome.web.vo.DeviceStateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/devicestate")
@Api(value = "设备状态表相关操作", tags = {"device-tag"})
@Slf4j
public class DeviceStatecontroller {

    @Resource
    private IDeviceStateService deviceStateService;
    @Resource
    private ILampService lampService;
    @Resource
    private IDoorService doorService;
    @Resource
    private ICurtainService curtainService;
    @Resource
    private ITelevisionService televisionService;
    @Resource
    private IHygrothermographService hygrothermographService;
    @Resource
    private IDetectorService detectorService;
    @Resource
    private IAirConditionerService airConditionerService;
    @Resource
    private INoticeService noticeService;
    @Resource
    private Imservice imservice;

    @PostMapping("/operation")//人操作！不是设备操作！
    public int operation(DeviceStateVo deviceStateVo) throws InterruptedException {
//        return deviceStateService.updateDevice(deviceStateVo);
        int id = deviceStateService.updateDevice(deviceStateVo);
        int time = 10;
        while(true){
            Thread.sleep(300);
            time--;
            int state = deviceStateService.getById(id).getIsUpdate();
            if(state==1||time==0){
                deviceStateService.removeById(id);
                return state;
            }
        }
    }

    @GetMapping("/lampupdate")
    @Transactional
    @ApiOperation(value = "所有设备的更新状态操作", notes = "提设备上传状态")
    public List<DeviceState> updateById(Integer id, Integer state, String content) {
        Lamp lamp = lampService.getById(id);
        lamp.setState(state);
        lamp.setContent(content);
        lampService.updateById(lamp);
        return deviceStateService.findOperationList(id,1);
    }

    @GetMapping("/doorupdate")
    @Transactional
    @ApiOperation(value = "所有设备的更新状态操作", notes = "提设备上传状态")
    public List<DeviceState> doorupdateById(Integer id, Integer state, String content) throws IOException {

        Door door = doorService.getById(id);
        door.setState(state);
        door.setContent(content);
        doorService.updateById(door);

        if ("isopen".equals(content)) {
            Notice notice = new Notice()
                    .setState(0)
                    .setUserId(door.getUserId())
                    .setContent(door.getName() + "的门开了!")
                    .setCreated(LocalDateTime.now())
                    .setDeviceId(id)
                    .setTypeId(3);
            noticeService.save(notice);
            imservice.sendmessage(door.getId().toString(),
                    "{\"msgtype\":\"NOTICE\",\"noticeId\":" + notice.getId() + "}");
        }

        return deviceStateService.findOperationList(id,3);
    }

    @GetMapping("/curtainupdate")
    @Transactional
    @ApiOperation(value = "所有设备的更新状态操作", notes = "提设备上传状态")
    public List<DeviceState> curtainupdateById(Integer id, Integer state, String content) {
        Curtain curtain = curtainService.getById(id);
        curtain.setState(state);
        curtain.setContent(content);
        curtainService.updateById(curtain);
        return deviceStateService.findOperationList(id,2);
    }

    @GetMapping("/televisionupdate")
    @Transactional
    @ApiOperation(value = "所有设备的更新状态操作", notes = "提设备上传状态")
    public List<DeviceState> televisionupdateById(Integer id, Integer state, String content) {
        Television television = televisionService.getById(id);
        television.setState(state);
        television.setContent(content);
        televisionService.updateById(television);
        return deviceStateService.findOperationList(id,5);
    }

    @GetMapping("/hygrothermographupdate")
    @Transactional
    @ApiOperation(value = "所有设备的更新状态操作", notes = "提设备上传状态")
    public List<DeviceState> hygrothermographupdateById(Integer id, Integer state, String content) throws IOException {
        Hygrothermograph hygrothermograph = hygrothermographService.getById(id);
        hygrothermograph.setState(state);
        hygrothermograph.setContent(content);
        hygrothermographService.updateById(hygrothermograph);

        String[] contents = content.split(";");
        String[] cs = contents[0].split(":");
        Double c = Double.parseDouble(cs[1]);
        if (c  >=30) {
            Notice notice = new Notice()
                    .setUserId(hygrothermograph.getUserId())
                    .setTypeId(7)
                    .setDeviceId(hygrothermograph.getId())
                    .setContent("温度过高")
                    .setCreated(LocalDateTime.now())
                    .setState(1);
            noticeService.save(notice);
            imservice.sendmessage(hygrothermograph.getUserId().toString(),
                    "{\"msgtype\":\"NOTICE\",\"noticeId\":" + notice.getId() + "}");

        }

        return deviceStateService.findOperationList(id,7);
    }

    @GetMapping("/detectorupdate")
    @Transactional
    @ApiOperation(value = "所有设备的更新状态操作", notes = "提设备上传状态")
    public List<DeviceState> detectorupdateById(Integer id, Integer state, String content) throws IOException {

        Detector detector = detectorService.getById(id);
        detector.setState(state);
        detector.setContent(content);
        detectorService.updateById(detector);

        Integer c = Integer.parseInt(content);
        if (c >= 320) {
            Notice notice = new Notice()
                    .setUserId(detector.getUserId())
                    .setTypeId(4)
                    .setDeviceId(detector.getId())
                    .setContent("有火情")
                    .setCreated(LocalDateTime.now())
                    .setState(1);
            noticeService.save(notice);
            imservice.sendmessage(detector.getUserId().toString(),
                    "{\"msgtype\":\"NOTICE\",\"noticeId\":" + notice.getId() + "}");
        }

        return deviceStateService.findOperationList(id,4);
    }

    @GetMapping("/detectorupdateWeb")
    @Transactional
    @ApiOperation(value = "web前端烟雾报警器上传操作", notes = "提设备上传状态")
    public List<DeviceState> detectorupdateWebById(Integer id, Integer state, String content) throws IOException {

        if (!"safe".equals(content)) {
            content="330";
        }
        if (!"danger".equals(content)){
            content="220";
        }
        Detector detector = detectorService.getById(id);
        detector.setState(state);
        detector.setContent(content);
        detectorService.updateById(detector);

        detectorService.updateById(detector);

        Integer c = Integer.parseInt(content);
        if (c >= 320) {
            Notice notice = new Notice()
                    .setUserId(detector.getUserId())
                    .setTypeId(4)
                    .setDeviceId(detector.getId())
                    .setContent("有火情")
                    .setCreated(LocalDateTime.now())
                    .setState(1);
            noticeService.save(notice);
            imservice.sendmessage(detector.getUserId().toString(),
                    "{\"msgtype\":\"NOTICE\",\"noticeId\":" + notice.getId() + "}");
        }

        return deviceStateService.findOperationList(id,4);
    }

    @GetMapping("/airconditionerupdate")
    @Transactional
    @ApiOperation(value = "所有设备的更新状态操作", notes = "提设备上传状态")
    public List<DeviceState> airconditionerupdateById(Integer id, Integer state, String content) {
        AirConditioner airConditioner = airConditionerService.getById(id);
        airConditioner.setState(state);
        airConditioner.setContent(content);
        airConditionerService.updateById(airConditioner);
        return deviceStateService.findOperationList(id,6);
    }

    @GetMapping("/done")
    @ApiOperation(value = "改变操作状态", notes = "前端提供id")
    public boolean changeUpdate(Integer id){
        DeviceState deviceState = deviceStateService.getById(id);
        deviceState.setIsUpdate(1);
        return deviceStateService.updateById(deviceState);
    }

}
